//
//  ECMaterailTypeEditViewController.m
//  ECERP.CRM
//
//  Created by huanggulong on 2018/11/22.
//  Copyright © 2018 Mac. All rights reserved.
//

#import "ECMaterailTypeEditViewController.h"
#import "ECMaterialTypeBigTypeHeaderView.h"
#import "ECShopMaterialType.h"
#import "ECMaterialTypeBigTypeHeaderView.h"
#import "GLBaseHTTPTool.h"

@interface ECMaterailTypeEditViewController ()<UITableViewDelegate,UITableViewDataSource,ECMaterialTypeBigTypeHeaderViewDelegate>

@property (nonatomic,strong) UITableView * tableView;

//@property (nonatomic,strong) NSMutableArray * dataArray;

@end

@implementation ECMaterailTypeEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"新增商品类别";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"新增大类" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightClick:)];
    [self setUI];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.materialTypes.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    ECShopMaterialType * type = [self.materialTypes objectAtIndex:section];
    if (type.isSelected) {
        return type.children.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.contentView.backgroundColor = @"#F7FCF8".toColor;
        cell.textLabel.textColor = @"#333333".toColor;
    }
    ECShopMaterialType * type1 = [self.materialTypes objectAtIndex:indexPath.section];
    ECShopMaterialType * type2 = [type1.children objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"    %@",type2.MaterialTypeName];
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
    {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    ECMaterialTypeBigTypeHeaderView * view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    if (view == nil) {
        view = [[ECMaterialTypeBigTypeHeaderView alloc] initWithReuseIdentifier:@"header"];
        view.contentView.backgroundColor = [UIColor whiteColor];
        view.delegate = self;
        [view.addButton addTarget:self action:@selector(smallTypeClick:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    view.contentView.tag = section;
    ECShopMaterialType * type = [self.materialTypes objectAtIndex:section];
    if (type.isSelected) {
        view.imageView.highlighted = YES;
    }else{
        view.imageView.highlighted = NO;
    }
    view.addButton.tag = section;
    view.nameLabel.text = type.MaterialTypeName;
    return view;
}


#pragma mark - ECMaterialTypeBigTypeHeaderViewDelegate
- (void)headerView:(ECMaterialTypeBigTypeHeaderView *)headerView section:(NSInteger)section{
    ECShopMaterialType * type = [self.materialTypes objectAtIndex:section];
    type.selected = !type.isSelected;
    [self.tableView reloadData];
}

#pragma mark - event
- (IBAction)rightClick:(id)sender{
    NSLog(@"%s",__FUNCTION__);
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"新增大类" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请输入类别名称";
    }];
    UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"确认" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        NSString * text = alert.textFields.firstObject.text;
        if (text.length == 0) {
            [MBProgressHUD showText:@"请输入类别名称" toView:self.view];
        }else{
            [self addBigTypeWithText:text];
        }
    }];
    [alert addAction:action1];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)addBigTypeWithText:(NSString *)text{
    GLHTTPParam * param = [[GLHTTPParam alloc] init];
    [param setSafeValue:@(0) forKey:@"PId"];
    [param setSafeValue:@(1.0) forKey:@"RadioPrice"];
    [param setSafeValue:text forKey:@"MaterialTypeName"];
    [param setSafeValue:@(1) forKey:@"Enabled"];
    [param setSafeValue:@"10001536" forKey:@"ManagerId"];
    [GLBaseHTTPTool baseRequestURL:@"api/Material/AddType" requestParam:param success:^(GLHTTPResult *result) {
        if (result.code == 0) {
            //成功拉取到 商品类型数据
            [MBProgressHUD showText:@"大类添加成功" toView:self.view];
        }else{
            [MBProgressHUD showText:result.msg toView:self.view];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD showText:@"数据拉取失败" toView:self.view];
    }];
}

- (void)addSmallTypeWithIndex:(NSInteger)index text:(NSString *)text{
    ECShopMaterialType * type = [self.materialTypes objectAtIndex:index];
    GLHTTPParam * param = [[GLHTTPParam alloc] init];
    [param setSafeValue:type.Id forKey:@"PId"];
    [param setSafeValue:type.MaterialTypeName forKey:@"BigMaterialTypeName"];
    [param setSafeValue:@(1.0) forKey:@"RadioPrice"];
    [param setSafeValue:text forKey:@"MaterialTypeName"];
    [param setSafeValue:@(1) forKey:@"Enabled"];
    [param setSafeValue:@(10001536) forKey:@"ManagerId"];
    [param setSafeValue:@"古龙" forKey:@"Manager"];
    [param setSafeValue:@"" forKey:@"SortCode"];
    [param setSafeValue:@"" forKey:@"Remark"];
    [param setSafeValue:@"" forKey:@"PurchaserId"];
    [param setSafeValue:@"" forKey:@"SuperId"];
//    [param addNullKeys:@[@"SortCode",@"Remark",@"PurchaserId",@"SuperId"]];
    [GLBaseHTTPTool jsonRequestURL:@"api/Material/AddType" requestParam:param success:^(GLHTTPResult *result) {
        if (result.code == 0) {
            //成功拉取到 商品类型数据
            [MBProgressHUD showText:@"小类添加成功" toView:self.view];
        }else{
            [MBProgressHUD showText:result.msg toView:self.view];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD showText:@"数据拉取失败" toView:self.view];
    }];
}

- (IBAction)smallTypeClick:(UIButton *)sender{
    NSInteger index = sender.tag;
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"新增小类" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请输入类别名称";
    }];
    UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"确认" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        NSString * text = alert.textFields.firstObject.text;
        if (text.length == 0) {
            [MBProgressHUD showText:@"请输入类别名称" toView:self.view];
        }else{
            [self addSmallTypeWithIndex:index text:text];
        }
    }];
    [alert addAction:action1];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
}



- (void)setUI{
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = @"#f4f4f4".toColor;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.and.bottom.offset(0);
        make.top.offset(0);
    }];
}

#pragma mark - getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.00000001)];
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.00000001)];
        //        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.separatorColor = @"#f4f4f4".toColor;
        _tableView.sectionHeaderHeight = 44;
        _tableView.sectionFooterHeight = 0.00001 * PX_RELATIVE375;
        _tableView.rowHeight = 44;
        //        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

@end
